package com.rmt.www.daoImpl;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;

import com.rmt.www.dao.FeatureArtifactDao;
import com.rmt.www.vo.FeatureArtifact;
import com.rmt.www.vo.TestArtifact;

public class FeatureArtifactDaoImpl implements FeatureArtifactDao {
	
	private JdbcTemplate jdbcTemplate;

	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}
	
	public void deleteFeatureArtifactById(int id) {
		jdbcTemplate.update("delete from featureartifact where id=?", new Object[]{id}, new int[]{java.sql.Types.INTEGER});
	}

	public FeatureArtifact getFeatureArtifactById(int id) {
		try {	
			return  (FeatureArtifact) this.jdbcTemplate.queryForObject("select * from featureartifact where id = ?",
					new Object[]{id}, new FeatureArtifactRowMapper());
		  }catch (final EmptyResultDataAccessException e) {
			  return null;
		  }
	}

	@SuppressWarnings("unchecked")
	public List<FeatureArtifact> getFeatureArtifactsByPId(int pid) {
		return (List<FeatureArtifact>)(this.jdbcTemplate.query("select * from featureartifact where pid=?",
				new Object[]{pid}, 
				new int[]{java.sql.Types.INTEGER}, new FeatureArtifactRowMapper()));
	}

	public int insertFeatureArtifact(FeatureArtifact artifact) {
		jdbcTemplate.update("insert into featureartifact(pid,title,authorid,version,priority,source,rational,createdtime,updaterid,updatedtime,status,description,cost,effort,risk,updatedtimes) " +
				"values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", 
		new Object[]{
			artifact.getPid(),
			artifact.getTitle(),
			artifact.getAuthorId(),
			artifact.getVersion(),
			artifact.getPriority(),
			artifact.getSource(),
			artifact.getRational(),
			artifact.getCreatedtime(),
			artifact.getUpdateUserId(),
			artifact.getUpdatedtime(),
			artifact.getStatus(),
			artifact.getDescription(),
			artifact.getCost(),
			artifact.getEffort(),
			artifact.getRisk(),
			artifact.getUpdatedtimes()
		},new int[] {
			java.sql.Types.INTEGER,
			java.sql.Types.VARCHAR,
			java.sql.Types.INTEGER,
			java.sql.Types.VARCHAR,
			java.sql.Types.VARCHAR,
			java.sql.Types.VARCHAR,
			java.sql.Types.VARCHAR,
			java.sql.Types.TIMESTAMP,
			java.sql.Types.INTEGER,
			java.sql.Types.TIMESTAMP,
			java.sql.Types.VARCHAR,
			java.sql.Types.VARCHAR,
			java.sql.Types.VARCHAR,
			java.sql.Types.INTEGER,
			java.sql.Types.VARCHAR,
			java.sql.Types.INTEGER
		});
		int lastInsertId = jdbcTemplate.queryForInt("select last_insert_id()");
		return lastInsertId;
	}

	public void updateFeatureArtifact(FeatureArtifact artifact) {
		jdbcTemplate.update("update featureartifact " +
				"set pid=?,title=?,authorid=?,version=?,priority=?,source=?,rational=?,createdtime=?,updaterid=?,updatedtime=?,status=?,description=?,cost=?,effort=?,risk=?,updatedtimes=? " +
				"where id=?", 
				new Object[]{
				//注意SQL语句中最后updatedtimes后面必须要有一个空格！
				artifact.getPid(),
				artifact.getTitle(),
				artifact.getAuthorId(),
				artifact.getVersion(),
				artifact.getPriority(),
				artifact.getSource(),
				artifact.getRational(),
				artifact.getCreatedtime(),
				artifact.getUpdateUserId(),
				artifact.getUpdatedtime(),
				artifact.getStatus(),
				artifact.getDescription(),
				artifact.getCost(),
				artifact.getEffort(),
				artifact.getRisk(),
				artifact.getUpdatedtimes(),
				artifact.getId()
			}, new int[] {
				java.sql.Types.INTEGER,
				java.sql.Types.VARCHAR,
				java.sql.Types.INTEGER,
				java.sql.Types.VARCHAR,
				java.sql.Types.VARCHAR,
				java.sql.Types.VARCHAR,
				java.sql.Types.VARCHAR,
				java.sql.Types.TIMESTAMP,
				java.sql.Types.INTEGER,
				java.sql.Types.TIMESTAMP,
				java.sql.Types.VARCHAR,
				java.sql.Types.VARCHAR,
				java.sql.Types.VARCHAR,
				java.sql.Types.INTEGER,
				java.sql.Types.VARCHAR,
				java.sql.Types.INTEGER,
				java.sql.Types.INTEGER
			});
	}

}
